Jumpstarting the Arduino 101 by Yining Shi & Sagar Mohite

Jumpstarting the Arduino 101 by Yining Shi & Sagar Mohite

Author:Yining Shi & Sagar Mohite
Language: eng
Format: epub, mobi
ISBN: 9781680453867
Published: 2017-08-27T16:00:00+00:00


main.js

Let’s start off by defining some constants. serviceUuid and name will allow this central device (your browser) to connect to the correct service on your peripheral 101 device with these two identifiers.

const serviceUuid ="19b10000-e8f2-537e-4f6c-d104768a1214"; const name = 'LEDCB'; var ledCharacteristic;

Next, define the connectTo101() function, which will be called when you click the Connect button in the UI. In this function, you first create an options object, which defines filters to search for your BLE peripheral device. Then you use the Web Bluetooth API to requestDevice and find your peripheral device with given filters.

The navigator.bluetooth.requestDevice() function returns a JavaScript Promise object. A Promise is an object that is often used for asynchronous communications with JavaScript, such as talking to a server. Once you receive a response, the callback function in the Promise’s then handler is executed. If there was an error, the callback function in the catch handler is executed. You can learn more about Promises by checking out this excellent explanation by Google: https://developers.google.com/web/fundamentals/getting-started/primers/promises.

Next, you connect to this device and then get the service with a given UUID from this device. Once you get the service, you get its characteristics using service.getCharacteristics().

Now that you have the characteristics, you assign the first one in the characteristics array to your ledCharacteristic variable.

function connectTo101() { let options = { filters: [{ services: [serviceUuid], name: name }] } console.log('Requesting Bluetooth Device...'); navigator.bluetooth.requestDevice(options) .then(device => { console.log('Got device', device.name); return device.gatt.connect(); }) .then(server => { console.log('Getting Service...'); return server.getPrimaryService(serviceUuid); }) .then(service => { console.log('Getting Characteristics...'); return service.getCharacteristics(); }) .then(characteristics => { console.log('Got LED Characteristic'); ledCharacteristic = characteristics[0]; }) .catch(error => { console.log('Argh! ' + error); }); }

Lastly, you define your writeTo101() function. In this function, you first select the value of your input with an ID of input-box and get its value. Then you convert the value to a buffer array of 8-bit unsigned integers. You can then use the writeValue function from the Web Bluetooth API to write this buffer to the ledCharacteristic that was selected in the connectTo101() function.

You’ve now written a characteristic value to your peripheral 101 device. The Arduino sketch you wrote earlier will handle the rest.

function writeTo101() { let inputValue = document.getElementById('input-box').value; let bufferToSend = Uint8Array.of(inputValue); ledCharacteristic.writeValue(bufferToSend); console.log('Writing '+ inputValue + ' to led Characteristic...'); }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.